/*
 * This file is part of the Try! Engine project.
 *
 * Copyright 2010-2011 Emanuele Bertoldi. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * You should have received a copy of the modified BSD License along with this
 * program. If not, see <http://www.opensource.org/licenses/bsd-license.php>
 */

#include "lightnode.h"

using namespace Try;

LightNode::LightNode(
        const String& name,
        float radius,
        float fov,
        const Color& color,
        const ResourceHandle& material,
        const String& lighting_ctx,
        const String& shadow_ctx,
        const Transform& t,
        SceneNode* parent
        )
    : SceneNode(h3dAddLightNode(SceneNode::rootNodeHandle(), name.c_str(), material, lighting_ctx.c_str(), shadow_ctx.c_str()))
{
    this->setParent(parent);
    this->setTransform(t);
    this->setRadius(radius);
    this->setFOV(fov);
    this->setColor(color);

    // FIXME: hard-coded params.
    h3dSetNodeParamI(m_node, H3DLight::ShadowMapCountI, 3 );
    h3dSetNodeParamF(m_node, H3DLight::ShadowSplitLambdaF, 0, 0.9f );
    h3dSetNodeParamF(m_node, H3DLight::ShadowMapBiasF, 0, 0.001f );
}

LightNode::~LightNode()
{
}

void LightNode::setRadius(float radius)
{
    h3dSetNodeParamF(m_node, H3DLight::RadiusF, 0, radius);
}

void LightNode::setFOV(float fov)
{
    h3dSetNodeParamF(m_node, H3DLight::FovF, 0, fov);
}

void LightNode::setColor(const Color& c)
{
    h3dSetNodeParamF(m_node, H3DLight::ColorF3, 0, c.r);
    h3dSetNodeParamF(m_node, H3DLight::ColorF3, 1, c.g);
    h3dSetNodeParamF(m_node, H3DLight::ColorF3, 2, c.b);
}

float LightNode::radius() const
{
    return h3dGetNodeParamF(m_node, H3DLight::RadiusF, 0);
}

float LightNode::fov() const
{
    return h3dGetNodeParamF(m_node, H3DLight::FovF, 0);
}

Color LightNode::color() const
{
    return Color(
                h3dGetNodeParamF(m_node, H3DLight::ColorF3, 0),
                h3dGetNodeParamF(m_node, H3DLight::ColorF3, 1),
                h3dGetNodeParamF(m_node, H3DLight::ColorF3, 2)
                );
}
